package com.jelly.config.shiro.token;

import com.jelly.web.modules.staff.user.domain.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.mgt.RealmSecurityManager;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.session.Session;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * <h4>功能:【token工具类】【创建人：QIAOLIANG】</h4>
 *
 * @时间:2018/7/3 0003 16:32
 * @备注:<h4></h4>
 */
public class TokenUtil {

    /**
     * 获取当前用户账号
     *
     * @return
     */
    public static String getAccount() {

        return getToken().getLoginAccount();
    }

    /**
     * 获取当前用户名称
     *
     * @return
     */
    public static String getNickName() {

        return getToken().getUserName();

    }

    /**
     * 获取当前用户的Session
     *
     * @return
     */
    public static Session getSession() {

        return SecurityUtils.getSubject().getSession();
    }

    /**
     * 获取当前登录的用户User对象
     *
     * @return
     */
    public static User getToken() {

        Object obj = SecurityUtils.getSubject().getPrincipal();
        User token = new User();
        if (obj != null) {
            token = (User) obj;
        }
        return token;
    }

    /**
     * 获取当前用户ID
     *
     * @return
     */
    public static String getUserId() {

        return getToken() == null ? null : getToken().getUserId();
    }

    /**
     * 从当前登录用户的Session里取值
     *
     * @param key
     * @return
     */
    public static Object getVal2Session(Object key) {

        return getSession().getAttribute(key);
    }

    /**
     * 获取验证码，获取一次后删除
     *
     * @return
     */
    public static String getYZM() {

        String code = (String) getSession().getAttribute("CODE");
        getSession().removeAttribute("CODE");
        return code;
    }


    /**<h4>功能：【获取当前登录用户权限】【2018/7/6 0006 18:40】【创建人：QIAOLIANG】</h4>
     * <h4></h4>
     * 
     */
    public static List<String> getPermission(){

        RealmSecurityManager rsm = (RealmSecurityManager) SecurityUtils.getSecurityManager();

        AuthorizingRealm shiroRealm = (AuthorizingRealm) rsm.getRealms().iterator().next();

        AuthorizationInfo authorizationInfo = shiroRealm.getAuthorizationCache().get(SecurityUtils.getSubject().getPrincipals());

        Collection<String> collection = authorizationInfo.getStringPermissions();

        List<String> list = new ArrayList<String>();

        if(null != collection && 0 < collection.size()){

            for (String stu : collection){

                list.add(stu);

            }

        }

        return list;

    }


    /**<h4>功能：【获取当前登录用户角色】【2018/7/6 0006 18:40】【创建人：QIAOLIANG】</h4>
     * <h4></h4>
     *
     */
    public static List<String> getRole(){

        RealmSecurityManager rsm = (RealmSecurityManager) SecurityUtils.getSecurityManager();

        AuthorizingRealm shiroRealm = (AuthorizingRealm) rsm.getRealms().iterator().next();

        AuthorizationInfo authorizationInfo = shiroRealm.getAuthorizationCache().get(SecurityUtils.getSubject().getPrincipals());

        Collection<String> collection = authorizationInfo.getRoles();

        List<String> list = new ArrayList<String>();

        if(null != collection && 0 < collection.size()){

            for (String stu : collection){

                list.add(stu);

            }

        }

        return list;

    }

    /**
     * 判断是否登录
     *
     * @return
     */
    public static boolean isLogin() {

        return null != SecurityUtils.getSubject().getPrincipal();
    }

    /**
     * 判断是否有角色
     *
     * @return
     */
    public static boolean isRole(String value) {

        return SecurityUtils.getSubject().hasRole(value);
    }

    public static User login(User user, Boolean rememberMe) {

        ShiroToken token = new ShiroToken(user.getLoginAccount(),user.getPassword());
        token.setRememberMe(rememberMe);
        SecurityUtils.getSubject().login(token);

        return getToken();
    }

    /**
     * 退出登录
     */
    public static void logout() {

        SecurityUtils.getSubject().logout();
    }

    /**
     * 把值放入到当前登录用户的Session里
     *
     * @param key
     * @param value
     */
    public static void setVal2Session(Object key, Object value) {

        getSession().setAttribute(key, value);

    }

}